package com.scqkxx.gamebox.activity;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.view.KeyEvent;
import android.view.View;

import com.avos.avoscloud.AVUser;
import com.scqkxx.gamebox.R;
import com.scqkxx.gamebox.activity.activities.ActivitiesFragment;
import com.scqkxx.gamebox.activity.activities.utils.ActivityUtil;
import com.scqkxx.gamebox.activity.game.GameFragment;
import com.scqkxx.gamebox.activity.home.HomeFragment;
import com.scqkxx.gamebox.activity.home.NavigationFragment;
import com.scqkxx.gamebox.activity.myself.LoginActivity;
import com.scqkxx.gamebox.activity.myself.MailCodeActivity;
import com.scqkxx.gamebox.activity.myself.MySelfSettingActivity;
import com.scqkxx.gamebox.activity.myself.MyselfFragment;
import com.scqkxx.gamebox.bean.EvenBusBean;
import com.scqkxx.gamebox.common.Const;
import com.scqkxx.gamebox.databinding.ActivityMainBinding;
import com.scqkzqtz.base.base.BaseActivity;
import com.scqkzqtz.base.library.utils.PreferenceUtils;
import com.scqkzqtz.base.library.utils.ToastUtils;
import com.scqkzqtz.basesdk.utils.ClickRadioButton;
import com.scqkzqtz.basesdk.utils.RadioTabManager;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends BaseActivity<ActivityMainBinding> {
    private List<Fragment> fragmentList = new ArrayList<>();
    private NavigationFragment navigationFragment = new NavigationFragment();

    @Override
    public int setLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public void init() {
        initView();
    }

    private void initView() {
        EventBus.getDefault().register(this);
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.layout_nav, navigationFragment);
        ft.commit();

        binding.ivNav.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                binding.drawerLayout.openDrawer(binding.layoutNav);
            }
        });
        binding.ivSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
        binding.drawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {
            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {

            }

            @Override
            public void onDrawerOpened(View drawerView) {

            }

            @Override
            public void onDrawerClosed(View drawerView) {
                if (navigationFragment != null) {
                    navigationFragment.hideItemList();
                }
            }

            @Override
            public void onDrawerStateChanged(int newState) {

            }
        });


        fragmentList.clear();
        fragmentList.add(new HomeFragment());
        fragmentList.add(new GameFragment());
        fragmentList.add(new ActivitiesFragment());
        fragmentList.add(new MyselfFragment());
        RadioTabManager radioTabManager = new RadioTabManager(getSupportFragmentManager(), fragmentList, R.id.main_tab_content, binding.groupTab, 0);
        ((ClickRadioButton) binding.groupTab.getChildAt(1)).setChecked(true);
        radioTabManager.setOnTabChangeListener((radioGroup, index) -> {
            if (index == 0 || index == 3) {
                binding.mainCardView.animate().translationY(binding.mainCardView.getHeight());
//                binding.mainCardView.setVisibility(View.GONE);
            }
        });
        binding.radio2.setOnClickListener(v -> ActivityUtil.switchTo(MainActivity.this, MySelfSettingActivity.class));
    }

    private Boolean isUpdateRequestHttp = false;//是否正在检测

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (isMainFastClick()) {
                finish();
                android.os.Process.killProcess(android.os.Process.myPid());    //获取PID
                System.exit(0);
            } else {
                if (binding.drawerLayout.isDrawerOpen(binding.layoutNav)) {
                    binding.drawerLayout.closeDrawer(binding.layoutNav);
                } else {
                    ToastUtils.showShortToast(this, "再按一次退出");
                }
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    private static long lastClickTime;

    public synchronized static boolean isMainFastClick() {
        long time = System.currentTimeMillis();
        if (time - lastClickTime < 1000) {
            return true;
        }
        lastClickTime = time;
        return false;
    }

    @Subscribe
    public void onEvenBus(EvenBusBean busBean) {
        binding.drawerLayout.closeDrawer(binding.layoutNav);
        if (busBean.getState() == 0) {
            ((ClickRadioButton) binding.groupTab.getChildAt(1)).setChecked(true);
//            binding.mainCardView.setVisibility(View.VISIBLE);
            binding.mainCardView.animate().translationY(0);
        } else if (busBean.getState() == 1) {//选中第一个按钮
            ((ClickRadioButton) binding.groupTab.getChildAt(0)).setChecked(true);
        } else if (busBean.getState() == 2) {
            AVUser.logOut();
            //退出登录
            PreferenceUtils.commitString("username", "");
            PreferenceUtils.commitString("createTime", "");
            PreferenceUtils.commitBoolean("isLogin", false);
            ActivityUtil.switchTo(this, LoginActivity.class);
            finish();
        }
    }

    @Subscribe
    public void onNav(String event) {
        if (Const.CLOSE_NAV.equals(event)) {
            binding.drawerLayout.closeDrawer(binding.layoutNav);
        } else if (Const.OPEN_NAV.equals(event)) {
            binding.drawerLayout.openDrawer(binding.layoutNav);
        }
    }
}
